﻿function addnewcatplain(type,operation) {
/* type = event/todo, operation = add/edit or an ID */
      if(type == 'event') {
      var pref;
        pref = 'ev';
      } else if(type == 'todo') {
        pref = 'task';
      } else {
        pref = type;
      }
      var cat = document.getElementById(operation+pref+'category').value;
      if(cat == "newcat") {
        var catname = prompt(JS_NEWCAT,JS_CATNAME);
        if(type == 'event') {
          pref = 'nce';
        } else if(type == 'todo') {
          pref = 'nct';
        }
        document.getElementById(operation+'newcat'+type).text = catname;
        document.getElementById(operation+'newcat'+type).value = pref+catname;
      }
}
function addnewcatfunc(type,operation) {
    return function(e) { addnewcatplain(type,operation) };
}
function showobj(obj1,obj2,obj3,obj4) {
  document.getElementById(obj1).style.display = 'block';
  document.getElementById(obj2).style.display = 'none';
  document.getElementById(obj3).style.display = 'none';
  document.getElementById(obj4).style.display = 'none';
  document.getElementById('recurtime').style.display = 'block';
}
function showobjfunc(par1,par2,par3,par4) {
    return function(e) { showobj(par1,par2,par3,par4) };
}
function hideall() {
  document.getElementById('daily').style.display = 'none';
  document.getElementById('weekly').style.display = 'none';
  document.getElementById('monthly').style.display = 'none';
  document.getElementById('yearly').style.display = 'none';
  document.getElementById('recurtime').style.display = 'none';
}
function toggle(item) {
    return function() {
      iobj = document.getElementById(item);
      if(iobj.style.display != 'none') {
        iobj.style.display = 'none';
        iobj.parentNode.childNodes[0].childNodes[0].className = 'filters-closed';
        savepref(item,'0');
      } else {
        iobj.style.display = 'block';
        iobj.parentNode.childNodes[0].childNodes[0].className = 'filters-open';
        savepref(item,'1');
      }
    };
}
function togglecheck(item,toggle) {
    return function() {
      if(typeof toggle == "undefined") {
        if (document.getElementById(item).checked) {
          document.getElementById(item).checked = false;
        } else {
          document.getElementById(item).checked = true;
        }
      } else {
        document.getElementById(item).checked = toggle;
      }
    };
}
function togglevis(type, item1, item2, item3, item4, item5, item6) {
        if(type == 'toggle') {
          if(document.getElementById(item1).style.display == 'none') {
            document.getElementById(item1).style.display = 'block';
            if(typeof item2 != "undefined") {
              document.getElementById(item2).style.display = 'none';
            }
            if(typeof item3 != "undefined") {
              if(document.getElementById(item3) !== null) {
                document.getElementById(item3).style.display = 'none';
              }
            }
            if(typeof item4 != "undefined") {
              document.getElementById(item4).style.display = 'none';
            }
            if(typeof item5 != "undefined") {
              document.getElementById(item5).style.display = 'none';
            }
            if(typeof item6 != "undefined") {
              document.getElementById(item6).style.display = 'none';
            }
          } else {
            document.getElementById(item1).style.display = 'none';
            if(typeof item2 != "undefined") {
              document.getElementById(item2).style.display = 'block';
            }
            if(typeof item3 != "undefined") {
              if(document.getElementById(item3) !== null) {
                document.getElementById(item3).style.display = 'block';
              }
            }
            if(typeof item4 != "undefined") {
              document.getElementById(item4).style.display = 'block';
            }
            if(typeof item5 != "undefined") {
              document.getElementById(item5).style.display = 'block';
            }
            if(typeof item6 != "undefined") {
              document.getElementById(item6).style.display = 'block';
            }
          }
        } else if(type == 'phase') {
            Effect.Phase(item1);
            if(typeof item2 != "undefined") {
              Effect.Phase(item2);
            }
            if(typeof item3 != "undefined") {
              if(document.getElementById(item3) !== null) {
                Effect.Phase(item3);
              }
            }
            if(typeof item4 != "undefined") {
              Effect.Phase(item4);
            }
            if(typeof item5 != "undefined") {
              Effect.Phase(item5);
            }
            if(typeof item6 != "undefined") {
              Effect.Phase(item6);
            }
        } else if(type == 'on') {
            document.getElementById(item1).style.display = 'block';
            if(typeof item2 != "undefined") {
              document.getElementById(item2).style.display = 'none';
            }
            if(typeof item3 != "undefined") {
              if(document.getElementById(item3) !== null) {
                document.getElementById(item3).style.display = 'none';
              }
            }
            if(typeof item4 != "undefined") {
              document.getElementById(item4).style.display = 'none';
            }
            if(typeof item5 != "undefined") {
              document.getElementById(item5).style.display = 'none';
            }
            if(typeof item6 != "undefined") {
              document.getElementById(item6).style.display = 'none';
            }
        } else if(type == 'off') {
            document.getElementById(item1).style.display = 'none';
            if(typeof item2 != "undefined") {
              document.getElementById(item2).style.display = 'block';
            }
            if(typeof item3 != "undefined") {
              document.getElementById(item3).style.display = 'block';
            }
            if(typeof item4 != "undefined") {
              document.getElementById(item4).style.display = 'block';
            }
            if(typeof item5 != "undefined") {
              document.getElementById(item5).style.display = 'block';
            }
            if(typeof item6 != "undefined") {
              document.getElementById(item6).style.display = 'block';
            }
        }
        if((item1 == 'calwrapper') && (document.getElementById('addform').style.display == 'block')) {
          document.getElementById('addevtitle').focus();
          setallday('main','add');
        }
}
function toggleall(type) {
    if(type == 'event') {
      if(document.getElementById('eventall').checked) {
        for(var i=0;i<eventcats.length;i++) {
          document.getElementById(eventcats[i]).checked = true;
        }
      } else {
        for(var i=0;i<eventcats.length;i++) {
          document.getElementById(eventcats[i]).checked = false;
        }
      }
    } else if(type == 'todo') {
      if(document.getElementById('todoall').checked) {
        for(var i=0;i<todocats.length;i++) {
          document.getElementById(todocats[i]).checked = true;
        }
      } else {
        for(var i=0;i<todocats.length;i++) {
          document.getElementById(todocats[i]).checked = false;
        }
      }
    }
}
function toggleallfunc(type, act, view, secs, requesttype, divtype) {
    return function(e) { toggleall(type); makerequest(act, view, secs, requesttype, divtype) };
}
function addondateplain(date) {
	togglevis('phase','calwrapper','addform');
	document.getElementById('addevstartdate').value = date;
	document.getElementById('addevenddate').value = date;
	e.preventDefault();
	return false;
}
function canceledit(id) {
    Effect.BlindUp('edit'+id, {duration:0.5} );
    Effect.BlindDown('todo'+id, {duration:0.5} );
    window[id+'taskeditor'].remove(); // remove the MCE editor
}
function markactive(menuid, id) {
        menuname = document.getElementById(menuid);
        as = menuname.getElementsByTagName('a');
        for(var i=0;i<as.length;i++) {
            as[i].className = 'inactivetab';
        }
        document.getElementById(id).className = 'activetab';
}
function dopostplain(buttonname, k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6) {
        var f = document.createElement('form');
        f.style.display = 'none'; 
        document.getElementById(buttonname).parentNode.appendChild(f);
        var h1 = document.createElement('input'); h1.type = 'hidden'; h1.name = k1; h1.value = v1; f.appendChild(h1);
        var h2 = document.createElement('input'); h2.type = 'hidden'; h2.name = k2; h2.value = v2; f.appendChild(h2);
        var h3 = document.createElement('input'); h3.type = 'hidden'; h3.name = k3; h3.value = v3; f.appendChild(h3);
        if(typeof k4 != "undefined") {
          var h4 = document.createElement('input'); h4.type = 'hidden'; h4.name = k4; h4.value = v4; f.appendChild(h4);
        }
        if(typeof k5 != "undefined") {
          var h5 = document.createElement('input'); h5.type = 'hidden'; h5.name = k5; h5.value = v5; f.appendChild(h5);
        }
        if(typeof k6 != "undefined") {
          var h6 = document.createElement('input'); h6.type = 'hidden'; h6.name = k6; h6.value = v6; f.appendChild(h6);
        }
        f.method = 'POST'; 
        f.action = 'index.php'; 
        f.submit();
        return false;
}

function setallday(id,type) {
    if((document.getElementById(type+'evallday') != null) || (document.getElementById(id+'allday') != null)) {
      if(id == 'main') {
        if(document.getElementById(type+'evallday').checked) {
            document.getElementById(type+'evstarthour').disabled = true;
            document.getElementById(type+'evstarthour').className = 'boxdisabled';
            document.getElementById(type+'evstartminute').disabled = true;
            document.getElementById(type+'evstartminute').className = 'boxdisabled';
            document.getElementById(type+'evendhour').disabled = true;
            document.getElementById(type+'evendhour').className = 'boxdisabled';
            document.getElementById(type+'evendminute').disabled = true;
            document.getElementById(type+'evendminute').className = 'boxdisabled';
        } else if(!document.getElementById(type+'evallday').checked) {
            document.getElementById(type+'evstarthour').disabled = false;
            document.getElementById(type+'evstarthour').className = 'small';
            document.getElementById(type+'evstartminute').disabled = false;
            document.getElementById(type+'evstartminute').className = 'small';
            document.getElementById(type+'evendhour').disabled = false;
            document.getElementById(type+'evendhour').className = 'small';
            document.getElementById(type+'evendminute').disabled = false;
            document.getElementById(type+'evendminute').className = 'small';
        }
      } else {
        if(document.getElementById(id+'allday').checked) {
            document.getElementById(id+'starthour').disabled = true;
            document.getElementById(id+'starthour').className = 'boxdisabled';
            document.getElementById(id+'startminute').disabled = true;
            document.getElementById(id+'startminute').className = 'boxdisabled';
            document.getElementById(id+'endhour').disabled = true;
            document.getElementById(id+'endhour').className = 'boxdisabled';
            document.getElementById(id+'endminute').disabled = true;
            document.getElementById(id+'endminute').className = 'boxdisabled';
        } else if(!document.getElementById(id+'allday').checked) {
            document.getElementById(id+'starthour').disabled = false;
            document.getElementById(id+'starthour').className = 'small';
            document.getElementById(id+'startminute').disabled = false;
            document.getElementById(id+'startminute').className = 'small';
            document.getElementById(id+'endhour').disabled = false;
            document.getElementById(id+'endhour').className = 'small';
            document.getElementById(id+'endminute').disabled = false;
            document.getElementById(id+'endminute').className = 'small';
        }
      }
    }
}
function setchecks(type) {
/**
@param string rowid the id of the row you want to make disappear
@param oject  opts  Effect options such as {duration:3}
*/
    return function() {
         var inputs = Array();
         var idx = 0;
         if(document.getElementById('listtable') != null) {
           var listtable = document.getElementById('listtable');
           var inputslisttable = listtable.getElementsByTagName('input');
           for(var t = 0;t < inputslisttable.length;t++) {
             if(inputslisttable[t].type == "checkbox") {
               inputs[idx] = inputslisttable[t];
               idx++;
             }
           }
         }
         if(document.getElementById('todotable') != null) {
           var todotable = document.getElementById('todotable');
           var inputstodotable = todotable.getElementsByTagName('input');
           for(var t2 = 0;t2 < inputstodotable.length;t2++) {
             if(inputstodotable[t2].type == "checkbox") {
               inputs[idx] = inputstodotable[t2];
               idx++;
             }
           }
         }

         if(type == 'all') {
           for(var t = 0;t < inputs.length;t++) {
             inputs[t].checked = true;
           }
         } else if(type == 'none') {
           for(var t = 0;t < inputs.length;t++) {
             inputs[t].checked = false;
           }
         } else if(type == 'invert') {
           for(var t = 0;t < inputs.length;t++){
             inputs[t].checked = !inputs[t].checked;
           }
         }
    };
}
function fadeTableRow(rowid){
    var row = document.getElementById(rowid);
    var cells = row.childNodes;
    for(i=0;i<cells.length;i++){
        if(cells[i].tagName == 'TD'){
            Effect.PhaseOut(cells[i], {duration:0.5} );
        }
    }
    Effect.Fade(row, {duration:0.5} );
}
function getposOffset(overlay, offsettype){
  var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
  var parentEl=overlay.offsetParent;
  while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}

function overlay(curobj, subobjstr, opt_position){
  if (document.getElementById) {
    var subobj = document.getElementById(subobjstr);
    if(subobj.style.display != 'block') {
        subobj.style.display = 'block';
    } else {
        subobj.style.display = 'none';
    }
    var xpos=getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0) 
    var ypos=getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)
    try {
      subobj.style.left=xpos+"px";
      subobj.style.top=ypos+"px";
    } catch(iesucks) { }
    return false;
  } else {
    return true;
  }
}

function overlayclose(subobj){
  document.getElementById(subobj).style.display = 'none';
}
function getEventsChartJSON() {
   return Object.toJSON(eventschartdata);
}
function getTaskChartJSON() {
   return Object.toJSON(eventschartdata);
} 